//
//  LSAlterView.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/9/10.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSSpeedSetView.h"

@implementation LSSpeedSetView {
    UIView *_maskView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self= [super initWithFrame:frame]) {
        
//        self.layer.cornerRadius = 6;
//        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI {
    
    UIView *backView = [[UIView alloc] init];
    backView.backgroundColor = [UIColor whiteColor];
    backView.layer.cornerRadius = 6;
    backView.layer.masksToBounds = YES;
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self).offset(40);
    }];
    
    UIImageView *iconImageView = [[UIImageView alloc] init];
    iconImageView.image = [UIImage imageNamed:@"LS-弹窗展示图标"];
    [self addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(1);
        make.size.mas_equalTo(CGSizeMake(110, 110));
    }];
    
    UILabel *titlelabel = [[UILabel alloc] init];
    [self addSubview:titlelabel];
    titlelabel.text = @"训练速度设置";
    titlelabel.textAlignment = NSTextAlignmentCenter;
    titlelabel.font = [UIFont systemFontOfSize:20];
    [titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(iconImageView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(130, 20));
    }];
    
    UILabel *line = [[UILabel alloc] init];
    [self addSubview:line];
    line.backgroundColor = [UIColor colorWithHex:0xededed];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.top.equalTo(titlelabel.mas_bottom).offset(10);
        make.height.mas_equalTo(0.5);
    }];
    
    LSSelectSpeedView *speed1 = [[LSSelectSpeedView alloc] init];
    speed1.tag = 1000;
    speed1.title.text = @"听力选择题";
    speed1.btn.tag = 1001;
    speed1.btn2.tag = 1002;
    speed1.btn3.tag = 1003;
    [speed1.btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [speed1.btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [speed1.btn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:speed1];
    [speed1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(300);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(40);
        make.top.equalTo(line.mas_bottom).offset(20);
    }];
    
    
    LSSelectSpeedView *speed2 = [[LSSelectSpeedView alloc] init];
    speed2.tag = 2000;
    speed2.title.text = @"跟读题";
    speed2.btn.tag = 2001;
    speed2.btn2.tag = 2002;
    speed2.btn3.tag = 2003;
    [speed2.btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [speed2.btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [speed2.btn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:speed2];
    [speed2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(300);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(40);
        make.top.equalTo(speed1.mas_bottom).offset(0);
    }];

    LSSelectSpeedView *speed3 = [[LSSelectSpeedView alloc] init];
    speed3.tag = 3000;
    speed3.title.text = @"朗读题";
    speed3.btn.tag = 3001;
    speed3.btn2.tag = 3002;
    speed3.btn3.tag = 3003;
    [speed3.btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [speed3.btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [speed3.btn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:speed3];
    [speed3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(300);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(40);
        make.top.equalTo(speed2.mas_bottom).offset(0);
    }];
}

- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    if (!_maskView) {
        UIView *maskView = [UIView new];
        _maskView = maskView;
        maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        [window addSubview:maskView];
        [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(window);
        }];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [maskView addGestureRecognizer:tap];
    }
  
    self.centerY = window.centerY - 50;
    self.centerX = window.centerX;
    [window addSubview:self];
    
    self.transform = CGAffineTransformMakeScale(0.01, 0.01);

    [UIView animateWithDuration:0.3 animations:^{
        
        self.transform = CGAffineTransformIdentity;
        _maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.5];
        
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    [_maskView removeFromSuperview];
    _maskView = nil;
    
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.resumeBlock();
    }];
}

- (void)btnAction:(UIButton*)sender {
    self.clickBlock(sender.tag);
    if (sender.tag<1005) {
        LSSelectSpeedView *view = [self viewWithTag:1000];
        UIButton *btn = [view viewWithTag:1001];
        UIButton *btn2 = [view viewWithTag:1002];
        UIButton *btn3 = [view viewWithTag:1003];
        [btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn2 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn3 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    } else if (sender.tag<2005) {
        LSSelectSpeedView *view = [self viewWithTag:2000];
        UIButton *btn = [view viewWithTag:2001];
        UIButton *btn2 = [view viewWithTag:2002];
        UIButton *btn3 = [view viewWithTag:2003];
        [btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn2 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn3 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    } else if (sender.tag<3005) {
        LSSelectSpeedView *view = [self viewWithTag:3000];
        UIButton *btn = [view viewWithTag:3001];
        UIButton *btn2 = [view viewWithTag:3002];
        UIButton *btn3 = [view viewWithTag:3003];
        [btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn2 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [btn3 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
   
    [sender setBackgroundImage:[UIImage imageNamed:@"LS-选择速度"] forState:UIControlStateNormal];
}

@end

@implementation LSSelectSpeedView

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    
    UILabel *title = [[UILabel alloc] init];
    self.title = title;
    title.textAlignment = NSTextAlignmentRight;
    title.font = [UIFont systemFontOfSize:17];
    title.textColor = [UIColor colorWithHex:0x434343];
    [self addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(90, 17));
    }];
    
    self.btn = [[UIButton alloc] init];
    [self addSubview:self.btn];
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn setTitle:@"慢速" forState:UIControlStateNormal];
    [self.btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(title.mas_right).offset(10);
        make.centerY.equalTo(title);
        make.size.mas_equalTo(CGSizeMake(64, 32));
    }];
//
    self.btn2 = [[UIButton alloc] init];
    [self addSubview:self.btn2];
    [self.btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn2 setTitle:@"中速" forState:UIControlStateNormal];
    [self.btn2 setBackgroundImage:[UIImage imageNamed:@"LS-选择速度"] forState:UIControlStateNormal];
    [self.btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(title);
        make.left.equalTo(self.btn.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(64, 32));
    }];
//
    self.btn3 = [[UIButton alloc] init];
    [self addSubview:self.btn3];
    [self.btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn3 setTitle:@"快速" forState:UIControlStateNormal];
    [self.btn3 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(title);
        make.left.equalTo(self.btn2.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(64, 32));
    }];
}

@end
